package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ResourceBundle;

/**
 *
 * @author siebz0r
 */
public class AquariumRow
		extends Row<Integer>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final int ID;
	private Integer _length, _width, _height;
	private String _name, _lighting, _heating, _filtering, _CO2;
	private AquariumTypeRow _type;
	private final Object LENGTH_LOCK,
			WIDTH_LOCK,
			HEIGHT_LOCK,
			NAME_LOCK,
			LIGHTING_LOCK,
			HEATING_LOCK,
			FILTERING_LOCK,
			CO2_LOCK,
			TYPE_LOCK;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public AquariumRow(ResultSet resultSet) throws SQLException
	{
		super(resultSet.getInt("id"));
		ID = resultSet.getInt("id");
		_length = resultSet.getInt("length");
		if (resultSet.wasNull())
		{
			_length = null;
		}
		_width = resultSet.getInt("width");
		if (resultSet.wasNull())
		{
			_width = null;
		}
		_height = resultSet.getInt("height");
		if (resultSet.wasNull())
		{
			_height = null;
		}
		_name = resultSet.getString("name");
		_type = Database.getAquariumTypeTable().getByPrimaryKey(resultSet.getInt("type"));
		_lighting = resultSet.getString("lighting");
		_heating = resultSet.getString("heating");
		_filtering = resultSet.getString("filtering");
		_CO2 = resultSet.getString("CO2");
		LENGTH_LOCK = new Object();
		WIDTH_LOCK = new Object();
		HEIGHT_LOCK = new Object();
		NAME_LOCK = new Object();
		LIGHTING_LOCK = new Object();
		HEATING_LOCK = new Object();
		FILTERING_LOCK = new Object();
		CO2_LOCK = new Object();
		TYPE_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getId()
	{
		return ID;
	}

	public String getName()
	{
		synchronized (NAME_LOCK)
		{
			return _name;
		}
	}

	public boolean setName(String name)
	{
		boolean result = false;
		synchronized (NAME_LOCK)
		{
			if (name == null ? _name != null : !name.equals(_name))
			{
				try
				{
					String sql = "UPDATE aquarium SET name = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (name != null)
					{
						st.setString(1, name);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_name = name;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public Integer getLength()
	{
		synchronized (LENGTH_LOCK)
		{
			return _length;
		}
	}

	public boolean setLength(Integer length)
	{
		boolean result = false;
		synchronized (LENGTH_LOCK)
		{
			if (length != _length)
			{
				try
				{
					String sql = "UPDATE aquarium SET length = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setObject(1, length, Types.INTEGER);
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_length = length;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public Integer getWidth()
	{
		synchronized (WIDTH_LOCK)
		{
			return _width;
		}
	}

	public boolean setWidth(Integer width)
	{
		boolean result = false;
		synchronized (WIDTH_LOCK)
		{
			if (width != _width)
			{
				try
				{
					String sql = "UPDATE aquarium SET width = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setObject(1, width, Types.INTEGER);
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_width = width;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public Integer getHeight()
	{
		synchronized (HEIGHT_LOCK)
		{
			return _height;
		}
	}

	public boolean setHeight(Integer heigth)
	{
		boolean result = false;
		synchronized (HEIGHT_LOCK)
		{
			if (heigth != _height)
			{
				try
				{
					String sql = "UPDATE aquarium SET height = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setObject(1, heigth, Types.INTEGER);
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_height = heigth;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public AquariumTypeRow getType()
	{
		synchronized (TYPE_LOCK)
		{
			return _type;
		}
	}

	public boolean setType(AquariumTypeRow type)
	{
		boolean result = false;
		synchronized (TYPE_LOCK)
		{
			if (type != _type)
			{
				try
				{
					String sql = "UPDATE aquarium SET type = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (type != null)
					{
						st.setInt(1, type.getId());
					}
					else
					{
						st.setNull(1, java.sql.Types.INTEGER);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_type = type;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getLighting()
	{
		synchronized (LIGHTING_LOCK)
		{
			return _lighting;
		}
	}

	public boolean setLighting(String lighting)
	{
		boolean result = false;
		synchronized (LIGHTING_LOCK)
		{
			if (lighting == null ? _lighting != null : !lighting.equals(_lighting))
			{
				try
				{
					String sql = "UPDATE aquarium SET lighting = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (lighting != null)
					{
						st.setString(1, lighting);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_lighting = lighting;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getHeating()
	{
		synchronized (HEATING_LOCK)
		{
			return _heating;
		}
	}

	public boolean setHeating(String heating)
	{
		boolean result = false;
		synchronized (HEATING_LOCK)
		{
			if (heating == null ? _heating != null : !heating.equals(_heating))
			{
				try
				{
					String sql = "UPDATE aquarium SET heating = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (heating != null)
					{
						st.setString(1, heating);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_heating = heating;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getFiltering()
	{
		synchronized (FILTERING_LOCK)
		{
			return _filtering;
		}
	}

	public boolean setFiltering(String filtering)
	{
		boolean result = false;
		synchronized (FILTERING_LOCK)
		{
			if (filtering == null ? _filtering != null : !filtering.equals(_filtering))
			{
				try
				{
					String sql = "UPDATE aquarium SET filtering = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (filtering != null)
					{
						st.setString(1, filtering);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_filtering = filtering;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getCO2()
	{
		synchronized (CO2_LOCK)
		{
			return _CO2;
		}
	}

	public boolean setCO2(String CO2)
	{
		boolean result = false;
		synchronized (CO2_LOCK)
		{
			if (CO2 == null ? _CO2 != null : !CO2.equals(_CO2))
			{
				try
				{
					String sql = "UPDATE aquarium SET CO2 = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (CO2 != null)
					{
						st.setString(1, CO2);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (st.executeUpdate() == 1)
					{
						_CO2 = CO2;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final AquariumRow other = (AquariumRow) obj;
		if (this.ID != other.ID)
		{
			return false;
		}
		if (this._length != other._length)
		{
			return false;
		}
		if (this._width != other._width)
		{
			return false;
		}
		if (this._height != other._height)
		{
			return false;
		}
		if ((this._name == null) ? (other._name != null) : !this._name.equals(other._name))
		{
			return false;
		}
		if ((this._lighting == null) ? (other._lighting != null) : !this._lighting.equals(other._lighting))
		{
			return false;
		}
		if ((this._heating == null) ? (other._heating != null) : !this._heating.equals(other._heating))
		{
			return false;
		}
		if ((this._filtering == null) ? (other._filtering != null) : !this._filtering.equals(other._filtering))
		{
			return false;
		}
		if ((this._CO2 == null) ? (other._CO2 != null) : !this._CO2.equals(other._CO2))
		{
			return false;
		}
		if (this._type != other._type && (this._type == null || !this._type.equals(other._type)))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 7;
		return hash;
	}

	@Override
	public String toString()
	{
		if (_name != null && !_name.isEmpty())
		{
			return _name;
		}
		else
		{
			return ResourceBundle.getBundle("carassius.resources.ObjectNames").getString("Nameless");
		}
	}
	//</editor-fold>
}
